package com.gadgets.framework.common.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.InputStream;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020-11-26
 * 
 */
public class Dom4jUtil {

    /**
     * 基于文件路径读取Dom
     * @param filePath  文件路径
     * @return          Dom结构
     */
    public static Document readDomByFile(String filePath) throws DocumentException {
        File file = new File(filePath);
        return readDomByFile(file);
    }

    /**
     * 基于文件读取Dom
     * @param file      文件路径
     * @return          Dom结构
     */
    public static Document readDomByFile(File file) throws DocumentException {
        SAXReader reader = new SAXReader();
        return reader.read(file);
    }

    /**
     * 基于字符串读取Dom
     * @param xmlStr    文件结构
     * @return          Dom结构
     */
    public static Document readDomByStr(String xmlStr) throws DocumentException {
        SAXReader reader = new SAXReader();
        return reader.read(xmlStr);
    }

    /**
     * 基于流服务Dom
     * @param stream                读取流
     * @return                      Dom结构
     */
    public static Document readDomByStream(InputStream stream) throws DocumentException{
        SAXReader reader = new SAXReader();
        return reader.read(stream);
    }


}
